﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// ChmInportFilesDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ChmInportFilesDialog : MetroWindow
    {
        public static string CustomTitle { get { return Globals.AppName + " - 引入外部文件"; } }

        /// <summary>
        /// 为 CHM 工程引入外部资源文件夹的对话框。
        /// </summary>
        /// <param name="subDirs">工作区下符合 “_xxx~”格式的外部资源目录。</param>
        /// <param name="directoryPathList">以分号分隔。为相对于工作区的短路径。</param>
        public ChmInportFilesDialog(System.IO.DirectoryInfo[] subDirs, string directoryPathList)
        {
            InitializeComponent();

            try
            {
                lbxOutFolders.Items.Clear();
                foreach (var di in subDirs)
                {
                    lbxOutFolders.Items.Add(new CheckBoxItem() { Title = di.Name, Tag = di.FullName, });
                }

                var checkedDirectories = directoryPathList.Split(new char[] { ';', }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var cd in checkedDirectories)
                {
                    foreach (var item in lbxOutFolders.Items)
                    {
                        var ci = item as CheckBoxItem;
                        if (ci == null) continue;

                        if (ci.Tag.ToString().IndexOf(cd) >= 0)
                        {
                            ci.IsChecked = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LMessageBox.ShowWarning(ex.Message);
            }
        }

        /// <summary>
        /// 指 CHM 引入的外部资源文件夹的列表，以分号间隔。
        /// </summary>
        public string ImportedDirectories { get; set; } = "";

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            var sb = new StringBuilder();
            foreach (var item in lbxOutFolders.Items)
            {
                var ci = item as CheckBoxItem;
                if (ci == null || ci.IsChecked != true) continue;

                var relativePath = ci.Tag as string;
                if (relativePath.ToLower().StartsWith(Globals.PathOfWorkspace.ToLower()))
                {
                    relativePath = relativePath.Substring(Globals.PathOfWorkspace.Length);
                }
                sb.Append(relativePath);
                sb.Append(";");
            }

            this.ImportedDirectories = sb.ToString();

            this.DialogResult = true;
            this.Close();
            return;
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
            return;
        }

        private void BtnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in lbxOutFolders.Items)
            {
                var ci = item as CheckBoxItem;
                if (ci == null) continue;
                ci.IsChecked = true;
            }
        }

        private void BtnUnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in lbxOutFolders.Items)
            {
                var ci = item as CheckBoxItem;
                if (ci == null) continue;
                ci.IsChecked = false;
            }
        }

        private void BtnSwitchSelect_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in lbxOutFolders.Items)
            {
                var ci = item as CheckBoxItem;
                if (ci == null) continue;

                if (ci.IsChecked != true)
                {
                    ci.IsChecked = true;
                    continue;
                }

                ci.IsChecked = false;
            }
        }
    }
}
